package com.jtpay.ysfproductionprocess.provider;


import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.google.gson.Gson;
import com.jtpay.ysfproductionprocess.db.YSFDecryptColumn;
import com.jtpay.ysfproductionprocess.db.YSFEncryptColumn;
import com.jtpay.ysfproductionprocess.model.CryptModel;
import com.jtpay.ysfproductionprocess.model.EncryptModel;
import com.jtpay.ysfproductionprocess.model.RemoteTaskModel;
import com.jtpay.ysfproductionprocess.utils.LogUtil;
import com.jtpay.ysfproductionprocess.utils.PayContext;
import com.jtpay.ysfproductionprocess.utils.manager.ExceptionUtil;

/**
 * Description:
 * File: UnionPayProvider.java
 * Author: Administrator
 * Create: 2018/08/16 13:59
 */
public class UnionPayEncryptProvider {
    private static Gson gson = new Gson();

    /**
     * @param context   Context
     * @param taskModel RemoteTaskModel
     */
    public static synchronized void insertEncryptInfo(Context context, RemoteTaskModel taskModel) {
        deleteEncryptDb(context);
        try {
            ContentValues values = new ContentValues();
            values.put(YSFEncryptColumn._MONEY, taskModel.getMoney());
            values.put(YSFEncryptColumn._REMARK, taskModel.getReason());
            values.put(YSFEncryptColumn._TYPE, taskModel.getDbType());
            if (taskModel.getDbType() == 0) {
                values.put(YSFEncryptColumn._INPUT, taskModel.getUnionPayOrderId());
            }
            values.put(YSFEncryptColumn._IS_OK, 0);
            context.getContentResolver().insert(YSFEncryptColumn.CONTENT_URI,
                    values);
            LogUtil.log("insertEncryptInfo " + values.toString());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtil.getInstance().saveExceptionFile("insertEncryptInfo error " + e.getMessage());
        }
    }

    /**
     * @param context Context
     * @param remark  task remark
     * @return EncryptModel
     */
    public static synchronized CryptModel selectEncryptInfo(Context context, String remark, int type) {
        Cursor c = null;
        try {
            c = context.getContentResolver().query(YSFEncryptColumn.CONTENT_URI,
                    new String[]{YSFEncryptColumn._ID,
                            YSFEncryptColumn._INPUT,
                            YSFEncryptColumn._OUTPUT,
                            YSFEncryptColumn._MONEY,
                            YSFEncryptColumn._IS_OK,
                            YSFEncryptColumn._REMARK,
                    },
                    YSFEncryptColumn._IS_OK + "=? and "
                            + YSFEncryptColumn._TYPE + "=? and "
                            + YSFEncryptColumn._REMARK + "=?",
                    new String[]{"1" + "", type + "", remark}, "_id DESC limit " +
                            String.valueOf(PayContext.OFFICE_LIMIT_NUM));

            if (c != null && c.moveToNext()) {
                CryptModel bean = new CryptModel();
                bean.setId(c.getInt(c.getColumnIndexOrThrow(YSFEncryptColumn._ID)));
                bean.setMoney(c.getString(c.getColumnIndexOrThrow(YSFDecryptColumn._MONEY)));
                bean.setRemark(c.getString(c.getColumnIndexOrThrow(YSFDecryptColumn._REMARK)));
                bean.setEn_treated(c.getInt(c.getColumnIndexOrThrow(YSFEncryptColumn._IS_OK)));
                String object = c.getString(c.getColumnIndexOrThrow(YSFEncryptColumn._OUTPUT));
                EncryptModel encryptModel = gson.fromJson(object, EncryptModel.class);
                if (encryptModel == null) {
                    return null;
                }
                if (type == 1) {
                    LogUtil.log("selectEncryptInfo");
                    bean.setEn_cryptModel(encryptModel);
                    bean.setEn_orderId(null);
                } else {
                    bean.setEn_cryptModel(encryptModel);
                    bean.setEn_orderId(encryptModel.getEn_orderId());
                    LogUtil.log("selectEncryptInfo getEn_orderId = " + encryptModel.getEn_orderId());
                    bean.setOrderId(c.getString(c.getColumnIndexOrThrow(YSFEncryptColumn._INPUT)));
                }
                LogUtil.log("getEncryptInfo " + bean.toString());
                return bean;
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            ExceptionUtil.getInstance().saveExceptionFile(
                    "ProviderHelper selectEncryptInfo IllegalArgumentException = " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtil.getInstance().saveExceptionFile(
                    "ProviderHelper selectEncryptInfo Exception = " + e.getMessage());
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return null;
    }

    /**
     * @param context Context
     * @param model   RemoteTaskModel
     * @param type    remote type 1 order decrypt ,0 qr content
     */
    public static synchronized void insertDecryptInfo(Context context, CryptModel model, int type) {
        deleteDecryptDb(context);
        try {
            ContentValues values = new ContentValues();
            values.put(YSFDecryptColumn._MONEY, model.getMoney());
            values.put(YSFDecryptColumn._REMARK, model.getRemark());
            if (type == 1) {
                values.put(YSFDecryptColumn._INPUT, model.getEn_content());
            } else {
                values.put(YSFDecryptColumn._INPUT, model.getQr_content());
            }
            values.put(YSFDecryptColumn._TYPE, type);
            values.put(YSFDecryptColumn._IS_OK, 0);
            context.getContentResolver().insert(YSFDecryptColumn.CONTENT_URI,
                    values);
            LogUtil.log("insertDecryptInfo " + values.toString());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtil.getInstance().saveExceptionFile("insertDecryptInfo error " + e.getMessage());
        }
    }

    /**
     * @param context Context
     * @param remark  task remark
     * @return EncryptModel
     */
    public static synchronized CryptModel selectDecryptInfo(Context context, String remark, int type) {
        Cursor c = null;
        try {
            c = context.getContentResolver().query(YSFDecryptColumn.CONTENT_URI,
                    new String[]{YSFDecryptColumn._ID,
                            YSFDecryptColumn._OUTPUT,
                            YSFDecryptColumn._MONEY,
                            YSFDecryptColumn._IS_OK,
                            YSFDecryptColumn._TYPE,
                            YSFDecryptColumn._REMARK,
                    },
                    YSFDecryptColumn._IS_OK + "=? and "
                            + YSFDecryptColumn._TYPE + "=? and "
                            + YSFDecryptColumn._REMARK + "=?",
                    new String[]{"1", type + "", remark}, "_id DESC limit " +
                            String.valueOf(PayContext.OFFICE_LIMIT_NUM));

            if (c != null && c.moveToNext()) {
                CryptModel bean = new CryptModel();
                bean.setId(c.getInt(c.getColumnIndexOrThrow(YSFDecryptColumn._ID)));
                bean.setMoney(c.getString(c.getColumnIndexOrThrow(YSFDecryptColumn._MONEY)));
                bean.setRemark(c.getString(c.getColumnIndexOrThrow(YSFDecryptColumn._REMARK)));
                bean.setEn_treated(c.
                        getInt(c.getColumnIndexOrThrow(YSFDecryptColumn._IS_OK)));
                String object = c.getString(c.getColumnIndexOrThrow(YSFDecryptColumn._OUTPUT));
                if (type == 1) {
                    bean.setDe_content(object);
                } else {
                    bean.setDe_qr_content(object);
                }
                LogUtil.log("selectDecryptInfo " + bean.toString());
                return bean;
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            ExceptionUtil.getInstance().saveExceptionFile(
                    "ProviderHelper selectDecryptInfo IllegalArgumentException = " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtil.getInstance().saveExceptionFile(
                    "ProviderHelper selectDecryptInfo Exception = " + e.getMessage());
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return null;
    }

    /**
     * deleteDb
     *
     * @param context Context
     */
    public static void deleteDb(Context context) {
        try {
            deleteEncryptDb(context);
            deleteDecryptDb(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * deleteEncryptDb
     *
     * @param context Context
     * @return delete result
     */
    private static void deleteEncryptDb(Context context) {
        try {
            Uri uriEncrypt = YSFEncryptColumn.CONTENT_URI;
            ContentResolver contentResolver = context.getContentResolver();
            contentResolver.delete(uriEncrypt, null, null);
            LogUtil.log("deleteEncryptDb");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * deleteDecryptDb
     *
     * @param context Context
     */
    private static void deleteDecryptDb(Context context) {
        try {
            ContentResolver contentResolver = context.getContentResolver();
            Uri uriDecrypt = YSFDecryptColumn.CONTENT_URI;
            contentResolver.delete(uriDecrypt, null, null);
            LogUtil.log("deleteDecryptDb");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
